package org.crackjack.android.async;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;

/**
 * Async task with Progress Dialog. Show s progress dialog before background task and hides it after background task
 * finish.
 * 
 * @param <A>
 * @param <B>
 * @param <C>
 * 
 * @author Konstantin
 * @version $Rev: $
 */
public abstract class ProgressDialogAsyncTask<A, B, C> extends ChainAsyncTask<A, Integer, C> {
    protected ProgressDialog progressDialog;
    protected Activity activity;
    private boolean cancelale;
    private boolean isProgressBarDialog;
    private String dialogMessage;

    /**
     * 
     * @param activity
     * @param isProgressBarDialog if true - dialog shows progress bar with %
     */
    public ProgressDialogAsyncTask(Activity activity, String title) {
        this(activity, false, title, new ITaskFinishedHandler<C>() {
            @Override
            public void onTaskFinished(C result) {
                // Nothing to do
            }
        });
    }

    public ProgressDialogAsyncTask(Activity activity, boolean isProgressBarDialog, String dialogMessage,
            ITaskFinishedHandler<C> handler) {
        super(handler);
        this.activity = activity;
        this.isProgressBarDialog = isProgressBarDialog;
        this.dialogMessage = dialogMessage;
    }

    public void setCancelable(boolean cancelable) {
        this.cancelale = cancelable;
    }

    @Override
    protected void onPreExecute() {
        if (getProgressDialog() == null) {
            progressDialog = new ProgressDialog(activity);
        } else {
            progressDialog = getProgressDialog();
        }

        progressDialog.setTitle("Loading");
        progressDialog.setMessage(dialogMessage);
        progressDialog.setCancelable(cancelale);
        progressDialog.setCanceledOnTouchOutside(false);

        if (isProgressBarDialog) {
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        }

        progressDialog.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                cancel(true);
            }
        });
        progressDialog.show();
        System.gc();
    }

    protected abstract C saveDoInBackground(A... params);

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);

        // Modify progress bar here
        progressDialog.setProgress(values[0]);
    }

    @Override
    protected C doInBackground(A... params) {
        return saveDoInBackground(params);
    }

    @Override
    protected void onCancelled() {
        onPostExecute(null);
    }

    @Override
    protected void onPostExecute(C result) {
        if (!activity.isFinishing()) {
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        }

        super.onPostExecute(result);
    }

    protected ProgressDialog getProgressDialog() {
        return null;
    }
}